<?php

/**
 * Subclass for representing a row from the 'transaction_data' table.
 *
 * 
 *
 * @package lib.model
 */ 
class TransactionData extends BaseTransactionData
{
	public function checkAmountCyrrency()
	{
		$currency = $this->getRequestedCurrencyCode();
		$payCurrnecy = $this->getPaidCurrencyCode();
		$amount = $this->getRequestedAmountCt();
		$payamount = $this->getPaidAmountCt();
		if($currency == $payCurrnecy)
        {
            if($amount != $payamount)
            {
            	$diff = abs($amount - $payamount);
            	if($diff>sfConfig::get('app_amountdiff_pct', 10))
            	{
	            	$this->setStateId(MyConstants::STATE_FAILED);
	                $this->setStatusId(MyConstants::STATUS_FALSE_AMOUNT);
	                $this->save();
	                return false;
            	}
            }
        }else 
        {
            $arConverter = MyConstants::getCurrencyConverter();
            $toCurrency = $payCurrnecy;
            $factorKey = $currency . '_' . $toCurrency;
            $price = $payamount;
            $price2 = $amount;
            $per = 0;
            $diff = 0;
            if( !empty($arConverter[$factorKey]) && ($arConverter[$factorKey] != $currency) )
            {
                 $price2 = $request['amount'] * $arConverter[$factorKey];
                 $diff = abs($price2 - $price);
                 $per = ($diff * 100)/$price;
            }elseif(empty($arConverter[$factorKey]))
            {
            	throw new Exception('Currency not set');
            }
            if($diff>sfConfig::get('app_amountdiff_pct', 50))
            {
                $this->setStateId(MyConstants::STATE_FAILED);
                $this->setStatusId(MyConstants::STATUS_FALSE_AMOUNT);
                $this->save();
                return false;
            }
        }
        return true;
	}
}
